// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Copyright (C) 2008 Vignette Corporation  All rights reserved.
// THIS PROGRAM IS CONFIDENTIAL AND AN UNPUBLISHED WORK AND TRADE SECRET
// OF THE COPYRIGHT HOLDER, AND DISTRIBUTED ONLY UNDER RESTRICTION.
// EXCEPT AS EXPLICITLY STATED IN A WRITTEN AGREEMENT BETWEEN THE PARTIES,
// THE SOFTWARE IS PROVIDED AS-IS, WITHOUT WARRANTIES OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
// NONINFRINGEMENT, PERFORMANCE, AND QUALITY.
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

import com.vignette.community.client.services.common.RemoteObjectEntity;
import com.vignette.community.client.services.common.XAPIConstants;
import com.vignette.community.client.services.dataservice.Action;
import com.vignette.community.client.services.dataservice.ActionData;
import com.vignette.community.client.services.dataservice.CommunityDataServiceFactory;
import com.vignette.community.client.services.dataservice.CommunityDataServiceIf;
import com.vignette.community.client.services.dataservice.CreateNewAction;
import com.vignette.community.client.services.dataservice.Query;
import com.vignette.community.client.services.dataservice.RemoteObjectActionData;
import com.vignette.community.client.services.dataservice.RemoteObjectQuery;
import com.vignette.community.client.services.dataservice.RemoteObjectQueryCreate;
import com.vignette.community.client.services.dataservice.Response;
import com.vignette.community.client.services.dataservice.SimpleActionData;
import com.vignette.community.client.services.util.Util;

import javax.servlet.http.HttpServletResponse;


/**
 * Community Data example - working with Comments. </br></br> For this example
 * to work the <code>vgncommsvcs.properties</code> should be found in the
 * classpath and should contain an entry based on the following: </br> <code>
 * CommunityServer1=collab,1,&lt;community_server_host&gt;,&lt;community_server_port&gt;,/xapi/,/xapi/,com.vignette.community.client.services.security.VssSecurityHandler,vssKey=changeme
 * </code>
 */
public class CDataComments {

    public String createRemoteObject(CommunityDataServiceIf dservice, RemoteObjectEntity ro) {

        String ROName = "What is the most popular sport?";
        String RODescription = "Story comparing popular sports";

        Action createAction = new CreateNewAction();
        ActionData remoteActionData =
                new RemoteObjectActionData(ROName,
                        RODescription,
                        ro);
        RemoteObjectQueryCreate createQuery =
                new RemoteObjectQueryCreate(ro,
                        createAction,
                        remoteActionData,
                        false);
        Response response1 = dservice.query(createQuery);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {
            return Util.getRedirectOid(response1);
        }
        return null;
    }

    private void createCommentOnRemoteObject(CommunityDataServiceIf dservice,
                                             String oid) {
        Action createAction = new CreateNewAction();
        SimpleActionData simpleActionData = new SimpleActionData();
        simpleActionData.addParams(
                XAPIConstants.NameFieldName,
                "comment name");
        simpleActionData.addParams(
                XAPIConstants.ObjectFieldName,
                oid);
        simpleActionData.addParams(
                XAPIConstants.TypeFieldName,
                "comment");
        simpleActionData.addParams(
                "content",
                "this is the contents of the comment");
        Response response1 = dservice.execute(createAction, simpleActionData);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_MOVED_PERMANENTLY) {
            String commentOid = Util.getRedirectOid(response1);
        }
    }

    private void getRemoteObjectsByCommentCount(CommunityDataServiceIf dservice,
                                                RemoteObjectEntity ro) {
        // Clone the RO then set some fields to null
        // to allow us to query for a set of ROs.
        RemoteObjectEntity incompleteRO = new RemoteObjectEntity(ro);
        incompleteRO.setExternalObjectId(null);
        incompleteRO.setExternalObjectContext(null);

        String pageSize = "20";
        String pageNum = "1";

        Query query = new RemoteObjectQuery(incompleteRO);
        query.addAdditionalParam(
                XAPIConstants.INCLUDES,
                XAPIConstants.COMMENTS);
        query.addAdditionalParam(
                XAPIConstants.SORTING,
                XAPIConstants.COMMENTCOUNT);
        query.addAdditionalParam(
                XAPIConstants.SORT_DIRECTION,
                XAPIConstants.SORT_DIRECTION_DESC);
        query.addAdditionalParam(
                XAPIConstants.PAGINATION,
                pageSize);
        query.addAdditionalParam(
                XAPIConstants.PAGINATION,
                pageNum);
        Response response1 = dservice.query(query);
        if (response1 != null
                && response1.getResponseCode()
                == HttpServletResponse.SC_OK) {
            String responseXML = response1.getData();
            // Parse out the RemoteObjects from this XML response.
        }
    }

    public static void main(String[] args) {

        String communityServerId = "CommunityServer1";
        String currentUser = "admin";
        String password = null;

        CDataComments cdata = new CDataComments();

        CommunityDataServiceIf dservice =
                CommunityDataServiceFactory.getInstance()
                        .createCommunityDataService(communityServerId,
                                currentUser,
                                password);

        // Create a Remote Object
        String extobjid = "12345";
        String extobjcontext = "Sports Channel";
        String extsystemid = "1";
        String extsystemtype = "CMS";
        String extrealm = "News Site";
        String extobjtype = "Article";
        String otherval = XAPIConstants.EXT_OBJ_STATUS
                + "="
                + XAPIConstants.DEV_STATUS;

        RemoteObjectEntity ro = new RemoteObjectEntity(extobjid,
                extobjcontext, extsystemtype,
                extsystemid, extrealm, extobjtype,
                otherval);

        String oid = cdata.createRemoteObject(dservice, ro);
        cdata.createCommentOnRemoteObject(dservice, oid);
        cdata.getRemoteObjectsByCommentCount(dservice, ro);

    }


}
